package com.example.leetdemo.plan1;

public class Sword012 {

    public int pivotIndex(int[] nums) {
        if (nums.length == 0) {
            return 0;
        }
        int[] t1 = new int[nums.length];
        int[] t2 = new int[nums.length];
        t1[0] = nums[0];
        for(int i = 1; i < nums.length; i++) {
            t1[i] = t1[i - 1] + nums[i];
        }
        t2[nums.length - 1] = nums[t2.length - 1];
        for (int i = nums.length - 2; i >= 0; i--) {
            t2[i] = t2[i + 1] + nums[i];
        }
        if (nums.length > 1 && t2[1] == 0) {
            return 0;
        }
        for (int i = 1; i < nums.length - 1; i++) {
            if (t1[i - 1] == t2[i + 1]) {
                return i;
            }
        }
        if (nums.length > 1 && t1[nums.length - 2] == 0) {
            return nums.length - 1;
        }
        return -1;
    }

    public static void main(String[] args) {
        Sword012 sword012 = new Sword012();
        System.err.println(sword012.pivotIndex(new int[] {2,1,-1}));
    }
}
